<template>
  <div class="articledetail">
    <div class="header">
      <div class="left">
        <van-icon name="arrow-left back" @click="$router.back()" />
        <span class="iconfont iconnew new"></span>
      </div>
      <span :class="{ active: post.has_follow }" @click="followUser">{{
        post.has_follow ? "已关注" : "关注"
      }}</span>
    </div>
    <div class="detail">
      <div class="title">{{ post.title }}</div>
      <div class="desc">
        <!-- 访问undefined的属性值会报错，因此这里采用一个短路运算 -->
        <span>{{ post.user && post.user.nickname }}</span>
        <span>{{ post.create_date | singledateFormat }}</span>
      </div>
      <div v-if="post.type == 1" class="content" v-html="post.content"></div>
      <video
        v-if="post.type == 2"
        :src="axios.defaults.baseURL + post.content"
        controls
        poster="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSa5HRZVlnaXf_B3G9iDvDFQMnH7ouSdd1KYg&usqp=CAU"
      ></video>
      <div class="opt">
        <div class="like">
          <van-icon
            name="good-job-o"
            @click="postLike"
            :class="{ active: post.has_like }"
          />{{ post.like_length }}
        </div>
        <div class="chat"><van-icon name="chat" class="w" />微信</div>
      </div>
    </div>
    <!-- 精彩跟帖 -->
    <div class="keeps">
      <h2>精彩跟帖</h2>
      <div class="item">
        <div class="head">
          <img src="../assets/logo.png" alt />
          <div>
            <p>火星网友</p>
            <span>2小时前</span>
          </div>
          <span>回复</span>
        </div>
        <div class="text">文章说得很有道理</div>
      </div>
      <div class="more">更多跟帖</div>
    </div>
    <commentFooter :article="post"></commentFooter>
  </div>
</template>

<script>
import { articleDetail, postlike } from "@/apis/post.js";
import { singledateFormat } from "@/utils/myfilters.js";
import axios from "@/utils/myaxios.js";
import { user_follows, unuser_follows } from "@/apis/user.js";
import { Toast } from "vant";
import commentFooter from "@/components/commentFooter.vue";
export default {
  components: {
    commentFooter,
  },
  data() {
    return {
      post: {},
      axios,
    };
  },
  filters: {
    singledateFormat,
  },
  async mounted() {
    setTimeout(() => {
      window.scrollTo(0, 0);
    }, 0);
    // console.log(this.$route.params.id);
    let res = await articleDetail(this.$route.params.id);
    // console.log(res);
    if (res.status == 200) this.post = res.data.data;
    // console.log(this.post);
  },
  methods: {
    async followUser() {
      // console.log(this.post.user.id);
      try {
        let res;
        // 已关注
        if (this.post.has_follow) {
          res = await unuser_follows(this.post.user.id);
        } else {
          res = await user_follows(this.post.user.id);
        }
        this.post.has_follow = !this.post.has_follow;
        Toast.success(res.data.message);
      } catch {}
    },
    async postLike() {
      let res = await postlike(this.$route.params.id);
      // console.log(res);
      Toast.success(res.data.message);
      if (res.data.message == "点赞成功") {
        this.post.like_length++;
      } else {
        this.post.like_length--;
      }
      this.post.has_like = !this.post.has_like;
    },
  },
};
</script>

<style lang="less" scoped>
.articledetail {
  width: 100vw;
  .header {
    padding: 0 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #ccc;
    .left {
      height: 100%;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      vertical-align: middle;
      .van-icon {
      }
      > span {
        font-size: 50px;
        margin-left: 5px;
      }
    }
    > span {
      padding: 5px 15px;
      border: 1px solid #f00;
      background-color: #f00;
      color: #fff;
      text-align: center;
      border-radius: 15px;
      font-size: 13px;
      &.active {
        border: 1px solid #ccc;
        background-color: transparent;
        color: #000;
      }
    }
  }
  .detail {
    padding: 15px;
    .title {
      font-size: 18px;
      font-weight: bold;
      padding: 10px 0;
    }
    .desc {
      line-height: 30px;
      color: #999;
      font-size: 13px;
      span {
        padding-right: 15px;
      }
    }
    .content {
      text-indent: 2em;
      line-height: 24px;
      font-size: 15px;
      width: 100%;
      /deep/ img {
        width: 100%;
        display: block;
      }
    }
    video {
      width: 100%;
    }
    .opt {
      display: flex;
      justify-content: space-around;
      margin-top: 30px;
      .like,
      .chat {
        height: 25px;
        padding: 0 15px;
        font-size: 14px;
        line-height: 25px;
        text-align: center;
        border: 1px solid #ccc;
        border-radius: 15px;
      }
      .active {
        color: red;
      }
      .w {
        color: rgb(84, 163, 5);
      }
    }
  }
  .keeps {
    border-top: 5px solid #ddd;
    padding: 0 15px;
    > h2 {
      line-height: 50px;
      text-align: center;
    }
    .item {
      padding: 10px 0;
      border-bottom: 1px solid #ccc;
      .head {
        display: flex;
        justify-content: space-between;
        align-items: center;
        > img {
          width: 50/360 * 100vw;
          height: 50/360 * 100vw;
          display: block;
          border-radius: 50%;
        }
        > div {
          flex: 1;
          display: flex;
          flex-direction: column;
          margin-left: 10px;
          > span {
            font-size: 12px;
            color: #999;
            line-height: 25px;
          }
        }
        > span {
          color: #999;
          font-size: 13px;
        }
      }
      .text {
        font-size: 14px;
        color: #333;
        padding: 20px 0 10px 0;
      }
    }
    .more {
      width: 100px;
      height: 30px;
      line-height: 30px;
      text-align: center;
      border: 1px solid #ccc;
      border-radius: 15px;
      margin: 20px auto;
      font-size: 13px;
    }
  }
}
</style>